package com.gxa.teax.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
/**
 * @author Administrator
 */
@Api(tags = "订单列表导出")
@Controller
@RequestMapping("/export")
public class OrderExportController {

    @GetMapping("/order")
    @ApiOperation(value = "导出方法")
    public void export(@ApiIgnore HttpServletResponse response,String[] orderIds) throws Exception {
        /*List<OrderExport> all = new ArrayList<>();
        for (String orderId : orderIds) {
            OrderExport orderExport = managerService.orderIdExport(orderId);
            all.add(orderExport);
        }*/
        // 工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("订单列表");
        String fileName = "订单列表.xlsx";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"序号","订单编号", "货物类别", "商品数量","箱数","金额","重量","收件人","联系方式","配送人","派单时间","时效产品"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
            // 把字段名添加到表头行上
        }
        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:
        int rowNum = 1;
        //zhe all 是空
        /*for (OrderExport orderExport : all) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(orderExport.getOrderId());
            dataRow.createCell(2).setCellValue(orderExport.getSecondName());
            dataRow.createCell(3).setCellValue(orderExport.getCommodityNumber());
            dataRow.createCell(4).setCellValue(orderExport.getBoxes());
            dataRow.createCell(5).setCellValue(orderExport.getMoney());
            dataRow.createCell(6).setCellValue(orderExport.getWeight());
            dataRow.createCell(7).setCellValue(orderExport.getName());
            dataRow.createCell(8).setCellValue(orderExport.getPhone());
            dataRow.createCell(9).setCellValue(orderExport.getConame());
            dataRow.createCell(10).setCellValue(dateFormat.format(orderExport.getDispatchTime()));
            dataRow.createCell(11).setCellValue(orderExport.getCommodityName());
            rowNum++;
        }*/
        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
